﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Najak.Editor
{
    public abstract partial class EditorAction
    {
        public class ActionManager
        {
            static public Dictionary<string, ActionManager> Registry = new Dictionary<string, ActionManager>();
            
            static public ActionManager GetOrCreate(string Scene)
            {
                ActionManager am;
                if (!Registry.TryGetValue(Scene, out am))
                {
                    am = new ActionManager();
                    Registry.Add(Scene, am);
                }
                return am.Clone();
            }
            public const int MaxActions = 200;
				public Scene CurrentScene { get { return Scene.Instance; } }
            public List<EditorAction> AppliedActions = new List<EditorAction>();
            public int MaxActionQueueIndex { get { return (AppliedActions.Count - 1); } }
            public int ActionQueueIndex = -1;
            public bool IsDirty;

            private ActionManager()
            {
            }

            public ActionManager Clone()
            {
                ActionManager am = this.MemberwiseClone() as ActionManager;
                am.AppliedActions = new List<EditorAction>(am.AppliedActions);
                return am;
            }

            public void UndoLastAction()
            {
                if (ActionQueueIndex < 0)
                    return; // nothing to undo
                                
                EditorAction action = AppliedActions[ActionQueueIndex];
                action.Undo(CurrentScene);
                ActionQueueIndex--;
            }

            public void RedoLastAction()
            {
                if (ActionQueueIndex >= MaxActionQueueIndex)
                    return; // nothing else to redo

                

                ActionQueueIndex++;
                EditorAction action = AppliedActions[ActionQueueIndex];
                action.OnApply(CurrentScene);
            }


            public void AdjustAppliedActionLocations(int minColumn, int minRow)
            {
                if (minColumn == 0 && minRow == 0)
                    return;

                foreach (EditorAction a in AppliedActions)
                {
                    a.AdjustLocation(minColumn, minRow);
                }
            }
        }
    }
}
